// 环境切换，本地调试与线上配置切换数据库配置
const path = require('path');
const env = (process.env.NODE_ENV || 'development').toLowerCase()
const file = path.resolve(__dirname, env);

const Sequelize = require('sequelize')

try {
    const { mysqlOptions } = require(file);
    //Mysql连接对象
    const mySequelize = new Sequelize(mysqlOptions.database, mysqlOptions.user, mysqlOptions.password, {
        host: mysqlOptions.host,
        dialect: 'mysql',
        port: 3306,
        pool: { max: 10, min: 0, idle: 10000 },
        //设置时区
        timezone: '+08:00',
        dialectOptions: {
            typeCast: function (field, next) {
                // 转换时间
                if (field.type == 'DATETIME' || field.type == 'TIMESTAMP') {
                    return new Date(field.string());
                }
                return next();
            }
        },
        // 禁用日志
        logging: false,
        omitNull:false,
    });
    module.exports = { mySequelize }
} catch (err) {
    console.log('无法载入配置文件')
    throw err;
}